import { add_uiShow } from "./ui";
import { jl3D,jl2D } from "./jl";
import { lookAtP,attack_by_pd } from "./boss";

export const randint = function (min:number, max:number) {
    min = Math.ceil(min); // 确保min是整数
    max = Math.floor(max); // 确保max是整数
    return Math.floor(Math.random() * (max - min + 1)) + min; // 返回介于min和max之间的整数
};
export function small_emery() {
    

world.querySelectorAll('.假人').forEach((jr)=>{
    jr.hp = jr.maxHp = 300;
    jr.hurtList=[];
    jr.hurtList_player=[];//清空列表
    jr.onDie(async ()=>{
        jr.position.y-=100000;
        
        
        jr.hurtList_player.forEach(async (e)=>{
            e.entity.exp += 10;
            e.entity.coin += 10;
            if (e.entity.player)
                add_uiShow('经验*10\n金币*10',e.entity);
        })
        jr.hurtList=[];//清空列表
        jr.hurtList_player=[];//清空列表
        await sleep(5000);
        jr.hp=jr.maxHp;
        jr.position.y+=100000;
    })
});
world.querySelectorAll('.树鹅宝宝').forEach((jr)=>{
    jr.hp = jr.maxHp = 700;
    jr.hurtList=[];
    jr.hurtList_player=[];//清空列表
    const damages = setInterval(()=>{
        world.querySelectorAll('player').forEach((e)=>{
            if (jl3D(jr.position,e.position)<=12){
                lookAtP(jr,e);
                const motion = jr.motion.loadByName('attack'); 
                const tx=attack_by_pd(e,jr,'mesh/绿色光球.vb');
                tx.meshScale = new GameVector3(1/32,1/32,1/32);
                motion.play();
                const damageInterval = setInterval(() => {
                    world.querySelectorAll('.canhurt').forEach(e => {
                        /*
                        if (entity !== jq && 
                            entity.position.distanceTo(damageZone.position) < 5) {
                            entity.takeDamage(10); // 假设存在扣血方法
                        }
                        */
                        let a={x:tx.position.x,z:tx.position.z};
                        let b={x:e.position.x,z:e.position.z};
                        if (jl2D(a,b)<=2 && Math.abs(tx.position.y-e.position.y)<=6 && e!==jr && e!==tx && !e.hasTag('emery')){
                            e.takeDamage_by_entity(10,jr);
                        }
                    });
                }, 400);

                // 1秒后销毁
                setTimeout(() => {
                    clearInterval(damageInterval);
                    tx.destroy();
                }, 1000);
            }
        })
    },1000)
    jr.onDie(async ()=>{
        jr.position.y-=100000;
        
        jr.hurtList_player.forEach(async (e)=>{
            e.entity.exp += 30;
            e.entity.coin += 50;
            add_uiShow('经验*30\n金币*50',e.entity);
        })
        jr.hurtList=[];//清空列表
        jr.hurtList_player=[];//清空列表
        await sleep(5000);
        jr.hp=jr.maxHp;
        jr.position.y+=100000;
    })
});

world.querySelectorAll('.怪物王').forEach((jr)=>{
    jr.hp = jr.maxHp = 2000;
    jr.hurtList=[];
    jr.hurtList_player=[];//清空列表
    const damages = setInterval(()=>{
        let flag = false;

        if (Math.random()<0.6){
            world.querySelectorAll('player').forEach((e)=>{
                if (jl3D(jr.position,e.position)<=18 ){
                    flag = true;
                    lookAtP(jr,e);
                    
                    const tx=attack_by_pd(e,jr,'mesh/白色光球.vb');
                    tx.meshScale = new GameVector3(1/32,1/32,1/32);
                    const damageInterval = setInterval(() => {
                        world.querySelectorAll('.canhurt').forEach(e => {
                            /*
                            if (entity !== jq && 
                                entity.position.distanceTo(damageZone.position) < 5) {
                                entity.takeDamage(10); // 假设存在扣血方法
                            }
                            */
                            let a={x:tx.position.x,z:tx.position.z};
                            let b={x:e.position.x,z:e.position.z};
                            if (jl2D(a,b)<=2 && Math.abs(tx.position.y-e.position.y)<=6 && e!==jr && e!==tx && !e.hasTag('emery')){
                                e.takeDamage_by_entity(30,jr);
                            }
                        });
                    }, 200);

                    // 1秒后销毁
                    setTimeout(() => {
                        clearInterval(damageInterval);
                        tx.destroy();
                    }, 1000);
                }
            })
            if (flag){
                const motion = jr.motion.loadByName('attack1'); 
                motion.play();
            }
        }
        else {
            world.querySelectorAll('player').forEach((e)=>{
                if (jl3D(jr.position,e.position)<=8){
                    flag = true;
                    lookAtP(jr,e);
                    const damageInterval = setInterval(() => {
                        world.querySelectorAll('.canhurt').forEach(e => {
                            /*
                            if (entity !== jq && 
                                entity.position.distanceTo(damageZone.position) < 5) {
                                entity.takeDamage(10); // 假设存在扣血方法
                            }
                            */
                            let a={x:jr.position.x,z:jr.position.z};
                            let b={x:e.position.x,z:e.position.z};
                            if (jl2D(a,b)<=10 && Math.abs(jr.position.y-e.position.y)<=6 && e!==jr && !e.hasTag('emery')){
                                e.takeDamage_by_entity(30,jr);
                            }
                        });
                    }, 200);
                    setTimeout(()=>{
                        clearInterval(damageInterval);
                    },1000)
                        
                }
            })
            if (flag){
                const motion = jr.motion.loadByName('attack2'); 
                motion.play();
            }
        }
    },1000)
    jr.onDie(async ()=>{
        jr.position.y-=100000;
        
        jr.hurtList_player.forEach(async (e)=>{
            e.entity.exp += 50;
            e.entity.coin += 100;
            add_uiShow('经验*50\n金币*100',e.entity);
        })
        jr.hurtList=[];//清空列表
        jr.hurtList_player=[];//清空列表
        await sleep(5000);
        jr.hp=jr.maxHp;
        jr.position.y+=100000;
    })
});

world.querySelectorAll('.晴空雀').forEach((jr)=>{
    jr.hp = jr.maxHp = 6000;
    jr.hurtList=[];
    jr.hurtList_player=[];//清空列表
    const damages = setInterval(()=>{
        let flag = false;
        let randResult = randint(1,3);

        if (randResult === 2){
            world.querySelectorAll('player').forEach((e)=>{
                if (jl3D(jr.position,e.position)<=10 ){
                    flag = true;
                    lookAtP(jr,e);
                    
                    const tx=attack_by_pd(e,jr,'mesh/绿色光球.vb');
                    tx.meshScale = new GameVector3(1/32,1/32,1/32);
                    const damageInterval = setInterval(() => {
                        world.querySelectorAll('.canhurt').forEach(e => {
                            /*
                            if (entity !== jq && 
                                entity.position.distanceTo(damageZone.position) < 5) {
                                entity.takeDamage(10); // 假设存在扣血方法
                            }
                            */
                            let a={x:tx.position.x,z:tx.position.z};
                            let b={x:e.position.x,z:e.position.z};
                            if (jl2D(a,b)<=2 && Math.abs(tx.position.y-e.position.y)<=6 && e!==jr && e!==tx && !e.hasTag('emery')){
                                e.takeDamage_by_entity(100,jr);
                            }
                        });
                    }, 200);

                    // 1秒后销毁
                    setTimeout(() => {
                        clearInterval(damageInterval);
                        tx.destroy();
                    }, 1000);
                }
            })
            if (flag){
                const motion = jr.motion.loadByName('被动技'); 
                motion.play();
            }
        }
        else if (randResult === 1){
            world.querySelectorAll('player').forEach((e)=>{
                if (jl3D(jr.position,e.position)<=8){
                    flag = true;
                    lookAtP(jr,e);
                    const damageInterval = setInterval(() => {
                        world.querySelectorAll('.canhurt').forEach(e => {
                            /*
                            if (entity !== jq && 
                                entity.position.distanceTo(damageZone.position) < 5) {
                                entity.takeDamage(10); // 假设存在扣血方法
                            }
                            */
                            let a={x:jr.position.x,z:jr.position.z};
                            let b={x:e.position.x,z:e.position.z};
                            if (jl2D(a,b)<=10 && Math.abs(jr.position.y-e.position.y)<=6 && e!==jr && !e.hasTag('emery')){
                                e.takeDamage_by_entity(30,jr);
                            }
                        });
                    }, 200);
                    setTimeout(()=>{
                        clearInterval(damageInterval);
                    },1000)
                        
                }
            })
            if (flag){
                const motion = jr.motion.loadByName('普通攻击'); 
                motion.play();
            }
        }
    },1000)
    jr.onDie(async ()=>{
        jr.position.y-=100000;
        
        jr.hurtList_player.forEach(async (e)=>{
            e.entity.exp += 100;
            e.entity.coin += 200;
            add_uiShow('经验*100\n金币*200',e.entity);
            if (Math.random() < 0.005) {
                e.entity.add({id: 17, number: 1},true); // 掉落创世之剑制作图
            }
        })
        jr.hurtList=[];//清空列表
        jr.hurtList_player=[];//清空列表
        await sleep(5000);
        jr.hp=jr.maxHp;
        jr.position.y+=100000;
    })
});

}